package com.wdb007.venus.svc.biz.qrcode;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wdb007.venus.svc.dal.qrcode.dao.QrCodeCurrentInfoDAO;
import com.wdb007.venus.svc.dal.qrcode.dao.QrCodePoliciesLogDAO;
import com.wdb007.venus.svc.dal.qrcode.model.QrCodeCurrentInfoDO;
import com.wdb007.venus.svc.dal.qrcode.model.QrCodePoliciesLogDO;
import com.wdb007.venus.svc.facade.QrCodeService;
import com.wdb007.venus.svc.facade.qrcode.model.QrCodeCurrentInfoDTO;

@Service("qrCodeService")
public class QrCodeServiceImpl implements QrCodeService {
	
	@Autowired
	private QrCodeCurrentInfoDAO qrCodeCurrentInfoDAO;
	@Autowired
	private QrCodePoliciesLogDAO qrCodePoliciesLogDAO;
	
	@Override
	public String saveQrCode(String bookCaseId) {
		QrCodeCurrentInfoDO infoDo = new QrCodeCurrentInfoDO();
		infoDo.setBookcaseId(bookCaseId);
		infoDo.setGenerateTime(new Date());
		String shortKey = generateShortKey();
		infoDo.setShortKey(shortKey);
		QrCodeCurrentInfoDO info = qrCodeCurrentInfoDAO.get(bookCaseId);
		if(null == info) {
			qrCodeCurrentInfoDAO.insert(infoDo);
		}else {
			qrCodeCurrentInfoDAO.update(infoDo);
		}
		
		QrCodePoliciesLogDO qrCodePoliciesLogDO = new QrCodePoliciesLogDO();
		qrCodePoliciesLogDO.setBookcaseId(bookCaseId);
		qrCodePoliciesLogDO.setGenerateTime(new Date());
		qrCodePoliciesLogDO.setShortKey(shortKey);
		qrCodePoliciesLogDAO.insert(qrCodePoliciesLogDO);
		return shortKey;
	}
	
	private String getRandomString(int length) { //length表示生成字符串的长度  
	    String base = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";     
	    Random random = new Random();     
	    StringBuffer sb = new StringBuffer();     
	    for (int i = 0; i < length; i++) {     
	        int number = random.nextInt(base.length());     
	        sb.append(base.charAt(number));     
	    }     
	    return sb.toString();     
	 }
	private String generateShortKey() {
		String shortKey = getRandomString(6);
		int count = qrCodeCurrentInfoDAO.countByShortKey(shortKey);
		if(count != 0) {
			return generateShortKey();
		}
		return shortKey;
	}
	@Override
	public List<QrCodeCurrentInfoDTO> list(Integer policiesInterval) {
		List<QrCodeCurrentInfoDO> list = qrCodeCurrentInfoDAO.list(policiesInterval);
		if(CollectionUtils.isEmpty(list))
			return null;
		List<QrCodeCurrentInfoDTO> dtoList = new ArrayList<QrCodeCurrentInfoDTO>();
		for(QrCodeCurrentInfoDO info : list) {
			QrCodeCurrentInfoDTO infoDTO = new QrCodeCurrentInfoDTO();
			BeanUtils.copyProperties(info, infoDTO);
			dtoList.add(infoDTO);
		}
		return dtoList;
	}

	@Override
	public void removeQrCodePoliciesLog() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, -1);
		Date d = c.getTime();
		qrCodePoliciesLogDAO.removeByDate(d);
		
	}

}
